package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.Image;
import com.funmi.lizip.entity.dto.image.ImageTestRequest;
import com.funmi.lizip.entity.vo.ImageVo;
import com.funmi.lizip.service.DataService;


import com.funmi.lizip.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzy
 * @since 2022-11-02
 */
@Api(tags = "测报灯和孢子仪")
@RestController
@RequestMapping("/data")
public class DataController {


    @Autowired
    private ImageService imageService;
    @Autowired
    private DataService dataService;


    @ApiOperation(value = "新增测报灯")
    @PostMapping(value = {"/saveTelemeteringLamp"}, produces = {"application/json;charset=UTF-8"})
    public R saveTelemeteringLamp(@RequestBody HashMap<String, Object> stringObjectHashMap) {
        return dataService.saveTelemeteringLamp(stringObjectHashMap);


    }

    //    @ApiOperation(value = "根据设备id查询图片")
//    @ApiImplicitParam(name = "deviceSn",value = "设备编号",required = true)
//    @GetMapping("/{id}")
//    public R photo( @PathVariable Long deviceSn){
//        List<String> photo = dataService.getByImei(deviceSn);
//        return R.success().data(photo);
//    }
//
//    @ApiOperation(value = "根据设备id查询图片")
//    @ApiImplicitParam(name = "deviceSn", value = "设备编号", dataTypeClass = String.class, required = true)
//    @GetMapping("/{deviceSn}")
//    public R photo(@PathVariable() Long deviceSn) {
//        List<String> photo = dataService.getByImei(deviceSn);
//        return R.success().data(photo);
//    }


    @ApiOperation(value = "新增测报灯的图片")
    @PostMapping(value = {"/imageTelemeteringLamp"}, produces = {"application/json;charset=UTF-8"})
    public R imageTelemeteringLamp(@RequestBody HashMap<String, Object> stringObjectHashMap) {

        return dataService.imageTelemeteringLamp(stringObjectHashMap);
    }


    @ApiOperation(value = "新增孢子仪")
    @PostMapping(value = {"/saveRobeInstrument"}, produces = {"application/json;charset=UTF-8"})
    public R saveRobeInstrument(@RequestBody HashMap<String, Object> stringObjectHashMap) {

        return dataService.saveRobeInstrument(stringObjectHashMap);
    }


    @ApiOperation("新增孢子仪图片")
    @PostMapping(value = {"/imageRobeInstrument"}, produces = {"application/json;charset=UTF-8"})
    public R imageRobeInstrument(@RequestBody HashMap<String, Object> stringObjectHashMap) {
        return dataService.imageRobeInstrument(stringObjectHashMap);
    }


//    @ApiOperation("查询图片||测报灯全部返回||孢子仪返回每日最清晰的照片")
//    @GetMapping("")
//
//    public R listImage() {
//        HashMap<String,List<String>> photo = dataService.getPhoto();
//        return R.success().data(photo);
//    }




    @ApiOperation(value = "查询图片||测报灯全部返回||孢子仪返回每日最清晰的照片")
    @GetMapping("/{imei}")
    public R findPhoto(@PathVariable(required = false) Long imei,int page,int pageSize){
        HashMap<Object,List<String>> photo = dataService.findPhoto(imei, page, pageSize);

        ImageTestRequest imageTestRequest = new ImageTestRequest();
        List<String> list =new ArrayList<>();
        List<String> list1 =new ArrayList<>();



        List<String> stringList = photo.get("image");
        int size1 = stringList.size();
        List<String> stringList1 = photo.get("createAt");



        int size=stringList.size();
        int pageStart=page==1?0:(page-1)*pageSize;//截取的开始位置
        int pageEnd=size<page*pageSize?size:page*pageSize;//截取的结束位置
        if(size>pageStart){

            list =stringList.subList(pageStart, pageEnd);
        }

        int size2=stringList1.size();
        int pageStart1=page==1?0:(page-1)*pageSize;//截取的开始位置
        int pageEnd1=size2<page*pageSize?size2:page*pageSize;//截取的结束位置
        if(size>pageStart1){

            list1 =stringList1.subList(pageStart1, pageEnd1);
        }

     //    int totalPage=photo.size()/pageSize;

       // imageTestRequest.setMap(photo);
        imageTestRequest.setImages(list);
        imageTestRequest.setCreateAt(list1);
        imageTestRequest.setTotal(size1);
        imageTestRequest.setPages(page);
        imageTestRequest.setPageSize(pageSize);
        return R.success().data(imageTestRequest);

    }


}



